package cn.yr.common.core.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author Lang 1102076808@qq.com
 * @description 腾讯ip获取物理地址
 * @date 2020-06-26 10:10
 */
public class IpAddressUtil {

	public static String getAddress(String ip){
		String url = "https://apis.map.qq.com/ws/location/v1/ip?key=GXTBZ-VB5R6-Q5CSB-E3RWU-JEV4T-43BC7&ip="+ip;
		String res = HttpUtil.get(url);
		JSONObject jsonObject = JSONUtil.parseObj(res);
		if(jsonObject.getInt("status",1)!=0){
			return null;
		}
		JSONObject result = jsonObject.getJSONObject("result");
		JSONObject adInfo = result.getJSONObject("ad_info");
		return adInfo.getStr("nation")+adInfo.getStr("province")+adInfo.getStr("city")+adInfo.getStr("district");
	}

	public static void main(String[] args) {
		System.out.println(getAddress("61.186.97.59"));
	}
}
